package cn.kgc.easybuy.controller;

import cn.kgc.easybuy.annotaion.CheckPermission;
import cn.kgc.easybuy.pojo.BuyCar;
import cn.kgc.easybuy.service.BuyCarService;
import cn.kgc.easybuy.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: yingzi-wants-to-sleep
 * @description:
 * @author: 敖成
 * @create: 2025-09-18 16:34
 **/
@RestController
public class BuyCarController {
    @Autowired
    private BuyCarService buyCarService;

    @CheckPermission({"0","1"})
    @PostMapping("/selectBuyCarByPage")
    public AjaxResult selectBuyCarByPage(@RequestBody Map<String,Object> params) {
        return AjaxResult.success().add("pageInfo", buyCarService.selectBuyCarByPage(params));
    }

    @CheckPermission({"0","1"})
    @PostMapping("/addBuyCar")
    public AjaxResult addBuyCar(@RequestBody BuyCar buyCar) {
        buyCar.setCreateTime(new Date());
        if (buyCarService.addBuyCar(buyCar)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @PostMapping("/updateBuyCar")
    public AjaxResult updateBuyCar(@RequestBody BuyCar buyCar) {
        if (buyCarService.updateBuyCar(buyCar)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @GetMapping("/deleteBuyCar")
    public AjaxResult deleteBuyCar( int id) {
        if (buyCarService.deleteBuyCarById(id)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @GetMapping("/deleteBuyCarByUserId")
    public AjaxResult deleteBuyCarByUserId(int userId) {
        if (buyCarService.deleteBuyCarByUserId(userId)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @CheckPermission({"0","1"})
    @GetMapping("/addBuyCarNum")
    public AjaxResult addBuyCarNum(int userId,int pid) {
        return AjaxResult.success().add("buyCar", buyCarService.addBuyCarNum(userId,pid));
    }

    @CheckPermission({"0","1"})
    @GetMapping("/selectAllBuyCar")
    public AjaxResult selectAllBuyCar(int userId) {
        return AjaxResult.success().add("allBuyCar", buyCarService.selectAllBuyCar(userId));
    }
}
